package tools;

import funcktion.BookManage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BookAddGUI extends JFrame {
    private JTextField bookIdTextField;
    private JTextField bookNameTextField;
    private JTextField priceTextField;
    private JTextField totalCopiesTextField;
    private JTextField availableCopiesTextField;
    private JTextField statusTextField;
    private JButton saveButton;

    public BookAddGUI() {
        setTitle("图书信息");
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2, 10, 10));

        JLabel bookIdLabel = new JLabel("图书编号");
        bookIdTextField = new JTextField();
        JLabel bookNameLabel = new JLabel("图书名称");
        bookNameTextField = new JTextField();
        JLabel priceLabel = new JLabel("价格");
        priceTextField = new JTextField();
        JLabel totalCopiesLabel = new JLabel("总库存");
        totalCopiesTextField = new JTextField();
        JLabel availableCopiesLabel = new JLabel("可借数量");
        availableCopiesTextField = new JTextField();
        JLabel statusLabel = new JLabel("状态");
        statusTextField = new JTextField();

        saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveBookInfo();
            }
        });

        add(bookIdLabel);
        add(bookIdTextField);
        add(bookNameLabel);
        add(bookNameTextField);
        add(priceLabel);
        add(priceTextField);
        add(totalCopiesLabel);
        add(totalCopiesTextField);
        add(availableCopiesLabel);
        add(availableCopiesTextField);
        add(statusLabel);
        add(statusTextField);
        add(new JLabel());
        add(saveButton);

        pack();
        setLocationRelativeTo(null);
    }

    private void saveBookInfo() {
        String bookId = bookIdTextField.getText();
        String bookName = bookNameTextField.getText();
        double price = 0;
        int totalCopies = 0;
        int availableCopies = 0;
        String status = statusTextField.getText();


        if (bookId.isEmpty() || bookName.isEmpty() || status.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段。\n价格，总库存，可借数量只能为数字");
        }else {

            try {
                price = Double.parseDouble(priceTextField.getText());
                totalCopies = Integer.parseInt(totalCopiesTextField.getText());
                availableCopies = Integer.parseInt(availableCopiesTextField.getText());

                // 保存图书信息到数据库
                // ...

                // 检查必填字段是否为空

                try {
                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "lang520520");
                    PreparedStatement stmt = conn.prepareStatement("INSERT INTO Book (bookId, bookName, price, totalCopies, availableCopies, status) VALUES (?, ?, ?, ?, ?, ?)");
                    stmt.setString(1, bookId);
                    stmt.setString(2, bookName);
                    stmt.setDouble(3, price);
                    stmt.setInt(4, totalCopies);
                    stmt.setInt(5, availableCopies);
                    stmt.setString(6, status);

                    stmt.executeUpdate();

                    JOptionPane.showMessageDialog(this, "图书信息保存成功");

                    BookManage GUI = new BookManage();
                    GUI.setVisible(true);
                    setVisible(false);
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(this, "保存失败：" + e.getMessage());
                    e.printStackTrace();
                }

            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "请输入有效的数字");
                e.printStackTrace();
            }
        }
    }
}
